<template>
	<common-card type="detail" title="课题详情" :loading="loading">
		<detail-body :data="dataDetail"></detail-body>
		<div class="data_detail_wrapper" v-if="dataDetail.isSave === '1' && dataDetail.audit !== '3'">
			<div class="item">
				<div class="label">审核人：</div>
				<div class="content">{{ dataDetail.auditName }}</div>
			</div>
			<div class="item">
				<div class="label">审核状态：</div>
				<div class="content">
					<el-tag type="success" v-if="dataDetail.audit === '1'">已通过</el-tag>
					<el-tag type="danger" v-if="dataDetail.audit === '2'">已驳回</el-tag>
				</div>
			</div>
			<div class="item">
				<div class="label">审核意见：</div>
				<div class="content">{{ dataDetail.auditDesc || '无' }}</div>
			</div>
			<div class="item">
				<div class="label">审核时间：</div>
				<div class="content">{{ dataDetail.auditTime }}</div>
			</div>
		</div>
		<template v-if="dataDetail.audit === '1'">
			<div class="data_detail_button" v-if="userRole === 'member'">
				<el-button class="added" type="primary" icon="sw-icon-btn-added" @click="handleAddedExperiment()">新实验申请</el-button>
				<el-button class="write" type="warning" icon="sw-icon-btn-write" @click="handleWriteAchievement()">填写课题成果</el-button>
			</div>
			<div class="data_detail_button" v-if="userRole === 'principal' && dataDetail.auditId === userInfo.id">
				<el-button class="added" type="primary" icon="sw-icon-btn-added" @click="handleAddedSchedule()">添加实验安排</el-button>
			</div>
		</template>
		<template slot="button">
			<template v-if="userRole === 'member'">
				<el-button type="danger" icon="sw-icon-btn-added" v-hasPermi="'3cb709a9f85647c199c1efc56e459486'" @click="handleAddedData()">新建</el-button>
				<template v-if="dataDetail.isSave === '2'">
					<el-button type="warning" icon="sw-icon-edit" v-hasPermi="'050dd767c0f7416eb91dbe72f655d4d9'" @click="handleEditData()">编辑</el-button>
					<el-button type="danger" icon="sw-icon-delete" v-hasPermi="'c5b3af8b99f14d07af755734343a1372'" @click="handleDeleteData()">删除</el-button>
				</template>
			</template>
			<template v-if="userRole === 'principal'">
				<template v-if="!!dataDetail.audit && dataDetail.audit === '3'">
					<el-button type="success" icon="sw-icon-btn-audit" v-hasPermi="'0b1475564d1c4bcfa5a7017432b5c7ac'" @click="handleAuditData()">
						审核
					</el-button>
				</template>
			</template>
			<template v-if="userRole === 'manager'">
				<el-button type="success" icon="sw-icon-edit" v-hasPermi="'3bac79e763d948e6b27e9bf75740c878'" @click="handleEditData()">编辑</el-button>
				<el-button type="danger" icon="sw-icon-delete" v-hasPermi="'c5b3af8b99f14d07af755734343a1372'" @click="handleDeleteData()">删除</el-button>
			</template>
		</template>
	</common-card>
</template>

<script>
import mixins from './mixins';
import detailBody from './body';
import { mapGetters } from 'vuex';
import commonCard from '../commonCard';
import { topicDetail, batchDeleteData } from '@/api/business/tbTopic';
export default {
	mixins: [mixins],
	components: { commonCard, detailBody },
	props: {
		userRole: { type: String, default: '' },
		dataType: { type: String, default: '' }
	},
	data() {
		return {
			loading: false,
			dataDetail: {}
		};
	},
	computed: {
		...mapGetters(['userInfo']),
		dataId() {
			return this.$route.query.id || '';
		}
	},
	watch: {
		dataId: {
			handler(id) {
				this.loading = true;
				this.getDataDetail(id);
			},
			immediate: true
		}
	},
	methods: {
		//获取数据详情
		async getDataDetail(id) {
			let result = await topicDetail(id).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				let datas = this.$detailData(result.data);
				this.$set(datas, 'piList', this.returnCheckedPiData(datas.piList || []));
				this.$set(datas, 'fundsList', this.returnCheckedFund(datas.fundsList || []));
				this.$set(datas, 'collaList', this.returnCheckedPerson(datas.collaList || []));
				this.$set(datas, 'measureList', this.returnCheckedMeasure(datas.measureList || []));
				this.dataDetail = datas;
			}
		},

		handleAddedData() {
			this.$router.push(`/${this.userRole}/${this.dataType}/form`);
		},

		handleEditData() {
			let query = { id: this.dataDetail.id };
			this.$router.push({ path: `/${this.userRole}/${this.dataType}/form`, query });
		},

		handleDeleteData() {
			let that = this;
			that.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.deleteDataRequest();
						instance.confirmButtonLoading = false;
					}
					done();
				}
			}).catch(() => {});
		},

		//删除课题请求
		async deleteDataRequest() {
			let params = [this.dataDetail.id];
			let result = await batchDeleteData(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', '删除数据成功');
				this.$router.back();
			}
		},

		handleAuditData() {
			let query = { id: this.dataDetail.id };
			this.$router.push({ path: `/${this.userRole}/${this.dataType}/audit`, query });
		},

		handleAddedExperiment() {
			let query = { topicid: this.dataDetail.id };
			this.$router.push({ path: `/member/experiment/form`, query });
		},

		handleWriteAchievement() {
			let query = { topicid: this.dataDetail.id };
			this.$router.push({ path: `/member/achievement/form`, query });
		},

		handleAddedSchedule() {
			let query = { type: '1', id: this.dataDetail.id };
			this.$router.push({ path: `/principal/schedule/list`, query });
		}
	}
};
</script>

<style lang="scss" scoped></style>
